<template>
  <div>
    <!-- 入库管理 -->
    <el-card shadow="hover" style="margin-bottom: 20px">
      <div slot="header" class="clearfix">
        <span>入库管理</span>
      </div>
      <el-form ref="inboundForm" :model="inboundForm" :rules="inboundRules" label-width="120px" style="width: 500px">
        <el-form-item label="RFID信息" prop="rfidInfo">
          <el-input v-model="inboundForm.rfidInfo" placeholder="请输入RFID信息"></el-input>
        </el-form-item>
        <el-form-item label="货架ID" prop="shelfId">
          <el-input type="number" v-model.number="inboundForm.shelfId" placeholder="请输入货架ID"></el-input>
        </el-form-item>
        <el-form-item label="货架层数" prop="shelfLayer">
          <el-input type="number" v-model.number="inboundForm.shelfLayer" placeholder="请输入货架层数"></el-input>
        </el-form-item>
        <el-form-item label="货架格子ID" prop="shelfCell">
          <el-input type="number" v-model.number="inboundForm.shelfCell" placeholder="请输入货架格子ID"></el-input>
        </el-form-item>
        <el-form-item label="操作人ID" prop="operator">
          <el-input type="number" v-model.number="inboundForm.operator" placeholder="请输入操作人ID"></el-input>
        </el-form-item>
        <el-form-item label="对象级别" prop="level">
          <el-input type="number" v-model.number="inboundForm.level" placeholder="请输入对象级别 (1-高级/2-中级/3-初级)"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleInboundSubmit">提交入库</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 出库管理 -->
    <el-card shadow="hover">
      <div slot="header" class="clearfix">
        <span>出库管理</span>
      </div>
      <el-form ref="outboundForm" :model="outboundForm" :rules="outboundRules" label-width="120px" style="width: 500px">
        <el-form-item label="RFID信息" prop="rfidInfo">
          <el-input v-model="outboundForm.rfidInfo" placeholder="请输入待出库对象的RFID信息"></el-input>
        </el-form-item>
        <el-form-item label="操作人ID" prop="operator">
          <el-input type="number" v-model.number="outboundForm.operator" placeholder="请输入操作人ID"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="handleOutboundSubmit">提交出库</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import stockApi from '@/api/stock'

export default {
  name: 'ManualStock',
  data() {
    return {
      // 入库表单
      inboundForm: {
        rfidInfo: '',
        shelfId: null,
        shelfLayer: null,
        shelfCell: null,
        operator: 1, // 默认操作员ID, 实际应从用户信息获取
        level: null
      },
      // 出库表单
      outboundForm: {
        rfidInfo: '',
        operator: 1 // 默认操作员ID
      },
      // 表单验证规则
      inboundRules: {
        rfidInfo: [{ required: true, message: '请输入RFID信息', trigger: 'blur' }],
        shelfId: [{ required: true, message: '请输入货架ID', trigger: 'blur' }, { type: 'number', message: '货架ID必须为数字'}],
        shelfLayer: [{ required: true, message: '请输入货架层数', trigger: 'blur' }, { type: 'number', message: '货架层数必须为数字'}],
        shelfCell: [{ required: true, message: '请输入货架格子ID', trigger: 'blur' }, { type: 'number', message: '货架格子ID必须为数字'}],
        operator: [{ required: true, message: '请输入操作人ID', trigger: 'blur' }, { type: 'number', message: '操作人ID必须为数字'}]
      },
      outboundRules: {
        rfidInfo: [{ required: true, message: '请输入RFID信息', trigger: 'blur' }],
        operator: [{ required: true, message: '请输入操作人ID', trigger: 'blur' }, { type: 'number', message: '操作人ID必须为数字'}]
      }
    }
  },
  methods: {
    // 统一处理API响应
    handleApiResponse(res, successMessage, errorMessage) {
      /* if (res && (res.code === 0 || res.success === true)) { */
        if (res && (res.code === 200)) {
        this.$message.success(successMessage)
        return true
      } else {
        this.$message.error(res.message || errorMessage)
        return false
      }
    },
    // 提交入库
    handleInboundSubmit() {
      this.$refs.inboundForm.validate(valid => {
        if (valid) {
          stockApi.inbound(this.inboundForm).then(res => {
            if (this.handleApiResponse(res.data, '入库成功', '入库失败')) {
              this.$refs.inboundForm.resetFields()
            }
          }).catch(err => {
            this.$message.error('请求失败: ' + err.message)
          })
        }
      })
    },
    // 提交出库
    handleOutboundSubmit() {
      this.$refs.outboundForm.validate(valid => {
        if (valid) {
          // 出库API需要完整的DTO, 但核心是rfidInfo
          const outboundData = {
            ...this.inboundForm, // 复用一个结构, 但清空非必要字段
            rfidInfo: this.outboundForm.rfidInfo,
            operator: this.outboundForm.operator,
            shelfId: 0, // 根据文档，非必须字段给默认值
            shelfLayer: 0,
            shelfCell: 0,
            level: 0
          }
          stockApi.outbound(outboundData).then(res => {
            if (this.handleApiResponse(res, '出库成功', '出库失败')) {
              this.$refs.outboundForm.resetFields()
            }
          }).catch(err => {
            this.$message.error('请求失败: ' + err.message)
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>
